package com.babel.util.util;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
	
	public static final String SPECIAL_CHARACTER_REGEX = "[`~!@^(){}';',\\[\\]<>~！@￥（）{}【】‘；：”“’。，、]";
	
	public static String toCamelString(String sourceStr, String seporator) {
		if(seporator == null) return sourceStr;
		if(sourceStr.contains(seporator)) {
			String[] cols = sourceStr.split(seporator);
			StringBuilder retVal = new StringBuilder(cols[0]);
			for(int i=1;i<cols.length;i++) {
				retVal.append(cols[i].substring(0, 1).toUpperCase()).append(cols[i].substring(1));
			}
			return retVal.toString();
		}
		return sourceStr;
	}
	
	/**
	 * 转换成首字母大写的驼峰字符串
	 * @param sourceStr
	 * @param seporator
	 * @return
	 */
	public static String toCapitalizedCamelString(String sourceStr, String seporator) {
		return org.apache.commons.lang.StringUtils.capitalize(toCamelString(sourceStr, seporator));
	}
	
	public static String convertToPlaceHolder(int length) {
		StringBuilder retVal = new StringBuilder("");
		for(int i=0;i<length;i++) {
			if(i == 0) {
				retVal.append("?");
			} else {
				retVal.append(",?");
			}
		}
		return retVal.toString();
	}
	
	public static String listToString(List<String> list, String separator) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < list.size(); i++) {
			sb.append("'"+list.get(i)+"'").append(separator);
		}
		return sb.toString().substring(0, sb.toString().length() - 1);
	}
	
	/**
     * 过滤html标签，取得文本
     * @param content - 包含HTML标签的内容 
     * @author tovish
     * @return 不带HTML标签的文本内容
     */
    public static String removeHtmlTag(String content) {
    	 if(content == null || "".equals(content)){
    		 return "";
    	 }
    	 String regxpForHtml = "<([^>]*)>";
    	 Pattern pattern = Pattern.compile(regxpForHtml);   
         Matcher matcher = pattern.matcher(content);   
         StringBuffer sb = new StringBuffer();   
         boolean result1 = matcher.find();   
         while (result1) {   
             matcher.appendReplacement(sb, "");   
             result1 = matcher.find();   
         }   
         matcher.appendTail(sb);   
         return sb.toString().trim();   
    }
    
    public static String filterSpecialCharacter(String str) {
    	if(str == null) {
    		return null;
    	}
    	return str.replaceAll(SPECIAL_CHARACTER_REGEX, "");
    }
    
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }
}
